package com.entrogy.dao;

import com.entrogy.bean.PassRecordBean;
import com.entrogy.bean.StudentEntity;
import com.entrogy.bean.TimeEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface PassRecordMapper {

	@Select("select * from time where id=1")
	TimeEntity updateTime();

    @Select("select pp.parent_visitor_phone from " +
			"(select parent_visitor_phone,parent_visitor_id,id from student_information,parent_visitor_information where " +
				"(parent1_id=parent_visitor_id or parent2_id=parent_visitor_id) and student_thdid=#{id} and school_code=#{schoolCode}) " +
			"pp")
	List<String> getParentPhone(@Param("id") String id, @Param("schoolCode") int schoolCode);


	@Insert("insert into ${tableName}(person_id, person_name, machine_name, record_time, flag, device_id,visitor_per_id_number,school_code) values(#{id}, #{personName}, " +
			"#{machineName}, #{time}, #{flag}, #{deviceId},#{visitorPerIdNumber},#{schoolCode})")
	void insertIntoDataBase(@Param("tableName") String tableName, @Param("id") int id, @Param("personName") String personName, @Param("machineName") String machineName,  @Param("time") Date time, @Param("flag") int flag, @Param("deviceId") String deviceId,@Param("visitorPerIdNumber") String visitorPerIdNumber,@Param("schoolCode") int schoolCode);

	@Select("SELECT * FROM student_information WHERE student_thdid = #{stuId} and school_code = #{schoolCode}")
	StudentEntity getStudentBeanByThdid(@Param("stuId") int stuId,@Param("schoolCode") int schoolCode);

	@Update("CREATE TABLE ${tableName}(" +
			"`id` bigint(20) NOT NULL AUTO_INCREMENT," +
			"`record_id` int(11) DEFAULT NULL," +
			"`person_id` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL," +
			"`person_name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL," +
			"`visitor_per_id_number` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL," +
			"`school_code` int(45) DEFAULT NULL," +
			"`machine_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL," +
			"`record_time` datetime DEFAULT NULL," +
			"`device_id` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL," +
			"`temp` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT '0.0'," +
			"`flag` int(2) DEFAULT NULL," +
			"`system_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '在我的系统内的id'," +
			"PRIMARY KEY (`id`)" +
			") ENGINE=InnoDB AUTO_INCREMENT=716 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;")
	void createPassRecord(@Param("tableName") String tableName);
	
    @Insert("INSERT INTO pass_record_information(pass_record_time,pass_record_table,create_time) VALUES(#{todayDate},#{tableName},#{currentDateTime})")
	int addPassRecordInfor(@Param("todayDate") String todayDate, @Param("tableName") String tableName,@Param("currentDateTime") String currentDateTime);

	@Select("select count(1) from ${tableName} where person_id=#{studentThdidId} and record_time>=DATE_SUB(#{passTime},INTERVAL 3 MINUTE)")
    int countMultiplePassRecord(@Param("studentThdidId") int studentThdidId, @Param("passTime") Date passTime, @Param("tableName") String tableName);

	@Select(" select ps.id,si.id as systemId " +
			"from student_information si inner join ${tableName} ps on si.student_thdid=ps.person_id " +
			"and RIGHT(ps.machine_name,3)='dm2' and record_time>#{lastTweLveMin}" +
			"union all " +
			"select ps.id,si.id as systemId from student_information si " +
			"inner join ${tableName} ps on si.student_thdid_ai16_id=ps.person_id and RIGHT(ps.machine_name,3)='dm1' and record_time>#{lastTweLveMin}")
	List<PassRecordBean> listPassRecord(@Param("tableName") String tableName, @Param("lastTweLveMin") String lastTweLveMin);

	@Update("update ${tableName} set system_id=#{passRecord.systemId} where id=#{passRecord.id}")
	int updatePassRecord(@Param("tableName") String tableName, @Param("passRecord") PassRecordBean passRecord);

}
